<?php
/**
 * Created by PhpStorm.
 * User: wangwenfan
 * Date: 2018/8/2
 * Time: 14:16.
 */

namespace App\Admin\Controllers;

use App\Models\Base\LogVipRemain;
use App\Service\ChannelService;
use App\Service\ServerService;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class VipUserRateController extends Controller
{
    /**首页
     * @return \Encore\Admin\Layout\Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('game.data_statistic'));
            $content->description(trans('game.vip_user_retained'));
            $content->body($this->grid());
            $this->filterScript();
        });
    }

    protected function grid()
    {
        $grid = Admin::grid(LogVipRemain::class, function (Grid $grid) {
            $grid->model()->orderBy('date_time', 'desc');
            $get = request()->all();
            $type = isset($get['type']) ? $get['type'] : 0;
            if (!$type) {
                $grid->model()->where('type', 1);
            }
            $grid->date_time(trans('game.date'));
            if (2 == $type) {
                $grid->channel()->name(trans('game.channel'));
            }
            if (3 == $type) {
                $grid->server()->name(trans('game.server_name'));
            }
            $grid->new_pay_users(trans('game.new_pay_user'));
            $grid->two_rate(trans('game.two_day'))->display(function ($two_rate) {
                return floatPercent($two_rate);
            });
            $grid->three_rate(trans('game.three_day'))->display(function ($three_rate) {
                return floatPercent($three_rate);
            });
            $grid->four_rate(trans('game.four_day'))->display(function ($four_rate) {
                return floatPercent($four_rate);
            });
            $grid->five_rate(trans('game.five_day'))->display(function ($five_rate) {
                return floatPercent($five_rate);
            });
            $grid->six_rate(trans('game.six_day'))->display(function ($six_rate) {
                return floatPercent($six_rate);
            });
            $grid->seven_rate(trans('game.seven_day'))->display(function ($seven_rate) {
                return floatPercent($seven_rate);
            });
            $grid->fifteen_rate(trans('game.fifteen_day'))->display(function ($fifteen_rate) {
                return floatPercent($fifteen_rate);
            });
            $grid->thirty_rate(trans('game.thirty_day'))->display(function ($thirty_rate) {
                return floatPercent($thirty_rate);
            });
            $grid->updated_at(trans('game.updated_at'));
            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });
            $grid->filter(function ($filter) use ($type) {
                $filter->disableIdFilter();
                $params = request()->all();
                $filter->between('date_time', trans('game.date'))->datetime();
                $filter->equal('type', trans('game.type'))->radio([1 => trans('game.platform_all'), 2 => trans('game.channel'), 3 => trans('game.server_name')]);
                $filter->equal('channel_id', trans('game.channel'))->select(ChannelService::getChannelList());
                $filter->equal('server_id', trans('game.server_name'))->select(ServerService::getServerList());
                $fileter_data = [];
                if (1 == $type) {
                    $filter->setNotWhere(['server_id', 'channel_id']);
                } elseif (2 == $type) {
                    if (isset($params['channel_id']) && 99 == $params['channel_id']) {
                        $fileter_data = ['channel_id'];
                    }
                    $fileter_data[] = 'server_id';
                } else {
                    if (isset($params['server_id']) && 99 == $params['server_id']) {
                        $fileter_data = ['server_id'];
                    }
                    $fileter_data[] = 'channel_id';
                }
                $filter->setNotWhere($fileter_data);
            });
        });

        return $grid;
    }
}
